package structural.composite.security;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在安全模式下，文件夹就需要自己来实现文件管理的功能，若有多个文件夹，则需要各个文件夹类自己来
 * 按照统一的规则编写文件管理方法，没有强制限制方法名，导致问题的发生
 */
public class SecurityDirectory extends ISecurityFile {
    private Map<String, ISecurityFile> m_mapFile;
    private String m_strDirName;

    public SecurityDirectory(String strDirName) {
        this.m_strDirName = strDirName;
        this.m_mapFile = new HashMap<String, ISecurityFile>();
    }

    public void addFile(String strName, ISecurityFile file){
        System.out.println(this.m_strDirName + "添加了新文件: " + strName);
        this.m_mapFile.put(strName, file);
    }

    public void removeFile(String strName){
        System.out.println(this.m_strDirName + "删除了文件: " + strName);
        this.m_mapFile.remove(strName);
    }

    public List<ISecurityFile> getChild() {
        List<ISecurityFile> listFile = new ArrayList<ISecurityFile>();
        for (String strName: this.m_mapFile.keySet()) {
            listFile.add(this.m_mapFile.get(strName));
        }
        return listFile;
    }

    @Override
    public void printContent() {
        System.out.println("扫描了" + this.m_strDirName);
        for (String strName: this.m_mapFile.keySet()) {
            this.m_mapFile.get(strName).printContent();
        }
    }
}
